var $ = require('../../lib/jquery');
var util = require('./common/util');
var nav = require('./common/nav');
var _header = require('./common/header');
var template = require('../tpl/template');
var _nav = new nav();
var header = new _header();
var xssfilter = require('./common/xssfilter');
require('./common/doubleDate2.0');
var minHeight = require('./common/minHeight');
minHeight.init();
var baoxian={
  flag:true,
  name:'',
  idNumber:'',
  idType:'',
  form:{
    treatType: 1, //{1：门诊,2:住院}
    startDate:'',//
    endDate:'',//
    insuranceCode: '', //保险公司编号
    visitingHospitalCode: '', //医院编码
    bankCode: '', //银行编号
    bankName: '', //银行名
    bankCardNumber: '', //银行账户
    accountName: '', //账户名
    provinceCode: '', //省份编码
    
  },
  init:function(){
    this.getUserInfo();
    this.hospitalList();
    this.queryCanpany();
    this.bindevent();
    this.bankList();
    this.provinceList();
    
    
  },
//个人信息
  getUserInfo:function(){
    var _this=this;
    util.fetch('api/sblp/queryUserInfo').then(function(res){
      _this.name=res.data.name;
      _this.idType=res.data.idType;
      _this.idNumber=res.data.idCard
      $('.js_name').html(res.data.name);
      $('.js_cardType').html(res.data.idType);
      $('.js_cardId').html(res.data.idCard); 
      $('.js_sex').html(res.data.gender);
    })
  },
  //商保参保机构
  queryCanpany:function(){
    var _this=this;
    util.fetch('api/sblp/queryInsurance').then(function(res){
        let html = '',pic='';
        for (let i = 0; i < res.data.length; i++) {
          html += "<option value=" + res.data[i].insuranceCode + " >" + res.data[i].insuranceFullName + "</option>"
          pic += "<p class='pic js_check' data-id=" + res.data[i].insuranceCode + "></p>"

        }
        _this.form.insuranceCode = res.data[0].insuranceCode;
        $('.js_Insurance').html(html);
        $('.js_pics').html(pic);
        $('.js_check').on('click', function () {
          var index = $(this).data().id;
          _this.form.insuranceCode=index;
          $('.js_check').removeClass('picborder');
          $(this).addClass('picborder');
         
        })
    })
  },
  //医院
  hospitalList:function(){
    var _this=this;
    util.fetch('api/sblp/queryHospitalList').then(function(res){
      let html='';
      for(let i=0;i<res.data.length;i++){
        html += "<option value=" + res.data[i].visitingHospitalCode + " >" + res.data[i].visitingHospitalName + "</option>"

      }
      // res.data.forEach(item => {
      //   html += `<option value= ${item.visitingHospitalCode} >${item.visitingHospitalName}</option>`
      // })
        _this.form.visitingHospitalCode = res.data[0].visitingHospitalCode;
      
      $('.js_hospital').html(html);
    })
  },
//银行
  bankList:function(){
    var _this=this;
    util.fetch('api/sblp/queryBankList').then(function(res){
      let html = '';
      for (let i = 0; i < res.data.length; i++) {
        html += "<option value=" + res.data[i].bankCode + " >" + res.data[i].
        bankName + "</option>"

      }
      _this.form.bankCode = res.data[0].bankCode;
      _this.form.bankName = res.data[0].bankName;

      $('.js_bank').html(html);

  })
  },
  provinceList: function () {
    var _this = this;
    util.fetch('api/sblp/queryProvinceList').then(function (res) {
      let html = '';
      for (let i = 0; i < res.data.length; i++) {
        html += "<option value=" + res.data[i].provinceCode + " >" + res.data[i].
        provinceName + " </option>"

      }
      _this.form.provinceCode = res.data[0].provinceCode;

      $('.js_province').html(html);

    })
  },
  //
  application:function(){
    var _this=this;
    util.fetch('api/sblp/applicationProgress', {
          name: _this.name,
          idNumber:_this.idNumber ,
          idType: _this.idType,
          applicationId:'',
          insuranceCode:_this.form.insuranceCode,
        }).then(function (res) {
          var html='';
          $('.js_result').removeClass('hide');
          for(var i=0;i<res.data.length;i++){      
            html += "<div class='quer quer" + (res.data[i] && res.data[i].caseState  || '')+ "'  > <p class='row df'><span class='a a1'></span><span class='b'>申请人：</span><span cass='c'>" + (res.data[i] && res.data[i].userName || '')+ "</span></p><p class='row  df'><span class='a a2'></span><span class='b'>受理人：</span><span cass='c'>" + (res.data[i] && res.data[i].acceptName || '') + "</span></p> <p class='row  df'><span class='a a3'></span><span class='b'>受理日期：</span><span cass='c'>" + (res.data[i] && res.data[i].acceptTime || '') + "</span></p><p class='row df'><span class='a a4'></span><span class='b'>报案号:</span><span cass='c'>" + (res.data[i] && res.data[i].caseNum || '')  + "</span> </p><p class='row df'><span class='a a5'></span><span class='b'>总金额:</span><span cass='c'>" + (res.data[i] && res.data[i].sumMoney || '')  + "</span> </p><p class='row df'><span class='a a6'></span><span class='b'>赔付金额:</span><span cass='c'>" + (res.data[i] && res.data[i].claimMoney || '') + "</span> </p><p class='row df'><span class='a a7'></span><span class='b'>理赔完成时间 :</span><span cass='c'>" + (res.data[i] && res.data[i].endTime || '') + "</span> </p> </div>"
               
          }
          $('.js_lipei').html(html)

    }).fail(function (res) {
      if (res.code == 'USR108') {
        $('.word').removeClass('hide')
      }else{
        alert(res.msg)
      }
      
    })
  },
  bindevent:function(){
  var _this=this;
    $('.js_choice_type').on('click',function(){
      let index=$(this).parent().index();
      _this.form.treatType=Number(index)+1;
      $('.js_choice_type').removeClass('choiceCircle');
      $(this).addClass('choiceCircle');
     
    })
    $('.js_start_date').kuiDate({
      className: 'js_start_date',
      isDisabled: "2" // isDisabled为可选参数，“0”表示今日之前不可选，“1”标志今日之前可选 '2' 表示今日之后不可选
    });
     $('.js_end_date').kuiDate({
       className: 'js_end_date',
       isDisabled: "2" // isDisabled为可选参数，“0”表示今日之前不可选，“1”标志今日之前可选 '2' 表示今日之后不可选
     });
      $('.js_hospital').on('change', function () {
        _this.form.visitingHospitalCode = $(this).val()
      })
      $('.js_Insurance').on('change', function () {
        _this.form.insuranceCode = $(this).val()
      })
       $('.js_bank').on('change', function () {
         _this.form.bankCode = $(this).val();
         
         _this.form.bankName = $(this).find("option:selected").text();
       })
        $('.js_province').on('change', function () {
          _this.form.provinceCode = $(this).val()
        })
        $('.js_is_query').on('click',function(){
          $('.js_is_query').removeClass('chioceColor');
          $(this).addClass('chioceColor');
         let index=$(this).index();
         $('.contentRight').addClass('hide')
          $('.contentRight').eq(index).removeClass('hide');
        
        })
      $('.js_btn').on('click',function(){
        $('.js_result').addClass('hide');
        _this.application()
      })
    
       
    $('.js_submit').on('click',function(){
        var reg = /^(\d{16}|\d{19})$/g;
         _this.form.startDate = $('.js_start_date').val().replace(/-/g, '');
         _this.form.endDate = $('.js_end_date').val().replace(/-/g,'');
        _this.form.accountName = $('.js_accountName').val();
        _this.form.bankCardNumber = $('.js_bankCardNumber').val();
       
       
        if (_this.form.accountName == '' || _this.form.bankCardNumber == '' || _this.form.startDate == '' || _this.form.endDate=='' ) {
            alert('请把信息填全')
           
        } else if (!reg.test(_this.form.bankCardNumber)) {
            alert('银行卡输入有误')
            
        }
        else{
          if(_this.flag){
            $(this).addClass('color')
            _this.flag=false;
          util.fetch('api/sblp/applyClaim', _this.form, function (res) {
            $('.js_submit').removeClass('color')
            _this.flag=true;
            alert('申请成功，商保公司将尽快处理您的理赔申请')
          }).fail(function (res) {
            _this.flag=true;
            $('.js_submit').removeClass('color')
              alert(res.msg)
          })
        }

        }
        
        

    })
  
  },


}
baoxian.init();